package see.battle.object;

import see.battle.Const;
import see.battle.drawer.Drawer;

public class Ship implements MovingGameObject {

  private Drawer _drawer;
  private int _objectId = -1;
  private int _direction; 
  private float _position;
  private Background _bg;
  private long _lastUpdateTime;

  /**
   * @param drawer
   * @param shipType
   * @param direction
   * @param bg
   * @param steps
   */
  public Ship(Drawer drawer, int shipType, 
      int direction, Background bg, long creationTime, long currentTime) {
    _drawer = drawer;
    if (_drawer != null) _objectId = _drawer.getShipId(shipType);
    _direction = direction;
    _bg = bg;
    if (_direction == Const.fromLeftToRightDirection) {
      _position = Const.backgroundLeft - Const.shipWidth;
    } else {
      _position = Const.backgroundRight;
    }
    _lastUpdateTime = creationTime;
    update(currentTime);
  }

  public int get_direction() {
    return _direction;
  }

  public void set_direction(int direction) {
    _direction = direction;
  }

  public float get_position() {
    return _position;
  }

  //@Override
  public void draw() {
    if (_direction == Const.fromLeftToRightDirection) {
      _drawer.draw(_objectId, (_position + _bg.get_position()), 
          (Const.backgroundSeaLevel - Const.shipHeight));
    } else {
      _drawer.draw(_objectId + 1, (_position + _bg.get_position()), 
          (Const.backgroundSeaLevel - Const.shipHeight));
    }
  }	

  //@Override
  public void update(long currentTime) {
    long steps = (currentTime - _lastUpdateTime) / 
    Const.updateDelay;
    _position += Const.shipSpeed * _direction * steps;
    _lastUpdateTime += Const.updateDelay * steps;
  }
  
  public void stopForBlast() {
    _lastUpdateTime += Const.blastDuration;
  }
}
